/*
 * (C) Copyright 2016 Ymatou (http://www.ymatou.com/).
 * 
 * All rights reserved.
 */
package com.ymatou.iapi.userservice.parameter.enums;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * 用户类型
 * @author zhongwei
 *
 */
public enum UserTypeEnum {

    /**
     * 买家
     */
    Normal(0, "Normal"),

    /**
     * 买手
     */
    Seller(1, "Seller"),

    /**
     * 管理员
     */
    Admin(10, "Admin"),
    ;

    private Integer code;

    private String message;

    UserTypeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 通过代码获取枚举项
     *
     * @param code
     * @return
     */
    public static UserTypeEnum getByCode(Integer code) {
        for (UserTypeEnum enumItem : UserTypeEnum.values()) {
            if (enumItem.getCode() == code) {
                return enumItem;
            }
        }
        return null;
    }

    /**
     *通过枚举值获取枚举项
     * @param msg
     * @return
     */
    public static UserTypeEnum getByMessage(String msg) {
        for (UserTypeEnum enumItem : UserTypeEnum.values()) {
            if (enumItem.getMessage().equalsIgnoreCase(msg)) {
                return enumItem;
            }
        }
        return null;
    }

    /**
     * 使用@JSONCreator来指定构造函数来创建枚举对象
     * @param val
     * @return
     */
    public static UserTypeEnum getValue(String val){
        if(StringUtils.isNumeric(val)){
            return getByCode(NumberUtils.toInt(val));
        }else{
            return getByMessage(val);
        }
    }
}
